package com.example.security.config;/**
 * @Auther: liyipeng
 * @Date: 2020/1/8 14:12
 * @Description:
 */

import com.example.security.SecurityUtil.ThreadLocalContext;
import com.example.security.SecurityUtil.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>类名: ThreadLocalFilter</p>
 * <p>描述:TODO</p>
 * <p>创建人: liyp</p>
 * <p>创建时间: 2020/1/8 14:12</p>
 * <p>@version 2.0  </p>
 * <p>修改内容: ......</p>
 * <p>修改说明: ......</p>
 * <p>修改时间: ......</p>
 * <p>修改人: ......</p>
 * <p>每次修改，请增加上述修改信息说明</>
 */
@WebFilter(asyncSupported = true)
public class ThreadLocalFilter implements Filter{

    private static final Logger LOGGER = LoggerFactory.getLogger(ThreadLocalFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        LOGGER.debug("初始化本地线程参数过滤器");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String accountId = request.getHeader("accountId");
        String name = request.getHeader("name");
        String loginway = request.getHeader("loginway");
        UserInfo userInfo = null;
        try{
            ThreadLocalContext.install(accountId,name,loginway);
            filterChain.doFilter(request,response);
        }finally {
            ThreadLocalContext.clear();
        }
    }

    @Override
    public void destroy() {

    }
}
